package com.codeoss.framework.webide.controller;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.codeoss.framework.webide.request.ModelFormPageRequest;
import com.codeoss.framework.webide.response.ModelFormPageResponse;
import com.codeoss.framework.webide.service.IModelFormPageService;

@Controller
@RequestMapping("webide/modelformpage")
public class ModelFormPageController {
	@Autowired
	private IModelFormPageService modelFormPageService;
	
	@RequestMapping(value = "init", method = RequestMethod.GET)
	@ResponseBody
	public ModelFormPageResponse init(@RequestParam("tableName") String tableName){
		ModelFormPageResponse modelFormPageResponse=modelFormPageService.init(tableName);
		return modelFormPageResponse;
	}
	
	@RequestMapping(value = "dataSave", method = RequestMethod.POST)
	@ResponseBody
	public ModelFormPageResponse dataSave(@RequestBody ModelFormPageRequest request){
		ModelFormPageResponse modelFormPageResponse=modelFormPageService.dataSave(request);
		return modelFormPageResponse;
	}
	
	@RequestMapping(value = "dataExport", method = RequestMethod.GET)
	public void  dataExport(@RequestParam("tableName") String tableName,HttpServletResponse response)throws Exception{
		ModelFormPageResponse modelFormPageResponse=modelFormPageService.dataExport(tableName);
		response.reset();
		try {
			response.addHeader("Content-Disposition","attachment;filename="+new String((modelFormPageResponse.getFileName()).getBytes(),"ISO-8859-1"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		response.getWriter().write(modelFormPageResponse.getFileContent());
	}
	
	@RequestMapping(value = "syncTable", method = RequestMethod.POST)
	@ResponseBody
	public ModelFormPageResponse  syncTable(@RequestParam("tableName") String tableName){
		ModelFormPageResponse modelFormPageResponse=modelFormPageService.syncTable(tableName,true);
		return modelFormPageResponse;
	}
	
	@RequestMapping(value = "modelAccessSave", method = RequestMethod.POST)
	@ResponseBody
	public ModelFormPageResponse modelAccessSave(@RequestBody ModelFormPageRequest request){
		ModelFormPageResponse modelFormPageResponse=modelFormPageService.modelAccessSave(request);
		return modelFormPageResponse;
	}
	
	@RequestMapping(value = "generateCode", method = RequestMethod.POST)
	@ResponseBody
	public ModelFormPageResponse generateCode(@RequestBody ModelFormPageRequest request){
		ModelFormPageResponse modelFormPageResponse=modelFormPageService.generateCode(request);
		return modelFormPageResponse;
	}
}
